using System;
using System.Collections.Generic;
using System.ComponentModel;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

using Elephant.Utility;

using Elephant.Framework;
using Elephant.Framework.Services;

namespace Elephant.Utility.Components
{
    public class FreeLookCamera : Camera
    {
        Transform transform;

        float speed = 15.0f;
        float yaw, pitch, roll;

        public override void Initialize(bool addDependencies)
        {
            base.Initialize(addDependencies);

            transform = Owner.Find<Transform>();
        }

        public void Rotate(float x, float y, float z)
        {
            yaw += x;
            pitch += y;
            roll += z;

            Quaternion q1 = Quaternion.CreateFromAxisAngle(new Vector3(0, 1, 0), this.yaw);
            Quaternion q2 = Quaternion.CreateFromAxisAngle(new Vector3(1, 0, 0), this.pitch);
            Quaternion q3 = Quaternion.CreateFromAxisAngle(new Vector3(0, 0, 1), this.roll);

            transform.Orientation = q1 * q2 * q3;
        }

        public void Translate(Vector3 distance)
        {
            transform.Position +=
                Vector3.Transform(
                    distance,
                    Matrix.CreateFromQuaternion(transform.Orientation));
        }

        public override Matrix View
        {
            get
            {
                return
                    Matrix.Invert(
                        Matrix.CreateFromQuaternion(transform.Orientation) *
                        Matrix.CreateTranslation(transform.Position));
            }
        }

        public float Speed
        {
            get
            {
                return speed;
            }
            set
            {
                speed = value;
            }
        }
    }
}
